package com.vancouver.knows.ui.activity.citydiscount;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;

import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.base.BaseActivity;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.CityDiscountDetailInfo;
import com.vancouver.knows.business.bean.PromotionInfo;
import com.vancouver.knows.business.bean.UserInfo;
import com.vancouver.knows.business.service.impl.BHttpService;

/**
 * 
 * @author cxlin
 * 
 */
public class UseWelfareActivity extends BaseActivity {

	@Bind(R.id.toolbar)
	Toolbar toolbar;
	@Bind(R.id.toobarTitle)
	TextView toobarTitle;
	@Bind(R.id.welfareImage)
	ImageView welfareImage;
	@Bind(R.id.name)
	TextView name;
	@Bind(R.id.userId)
	TextView userId;
	@Bind(R.id.message)
	TextView message;
	@Bind(R.id.promotionDetail)
	TextView promotionDetail;
	@Bind(R.id.usingMethod)
	TextView usingMethod;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_use_welfare);

		Intent intent = getIntent();
		if (intent == null) {
			return;
		}

		PromotionInfo promotionInfo = (PromotionInfo) intent.getSerializableExtra("PromotionInfo");
		if (promotionInfo == null) {
			return;
		}
		String postId = intent.getStringExtra("postId");
		if (StringUtil.isEmptyString(postId)) {
			return;
		}

		BusinessModule bModule = imContext.getBusinessModule();
		CityDiscountDetailInfo cityDisocuntDetailInfo = bModule.getCacheManager().getDiscountDetailInfoMap()
				.get(postId);
		if (cityDisocuntDetailInfo == null) {
			return;
		}
		UserInfo userInfo = bModule.getCacheManager().getUserInfo();
		if (userInfo == null) {
			return;
		}

		toolbar.setTitle("");
		setSupportActionBar(toolbar);
		toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back));
		toobarTitle.setText(R.string.use_welfare);
		String url = "https://" + BHttpService.DOMAIN + "?page=qrcode&text=" + promotionInfo.getCode();
		Picasso.with(imContext).load(url).into(welfareImage);
		name.setText(cityDisocuntDetailInfo.getName());
		userId.setText((Integer.valueOf(userInfo.getUserID()) * 1285 + 280 - 2015) + "");
		message.setText(promotionInfo.getMessage());
		promotionDetail.setText(promotionInfo.getFullDesc());
		usingMethod.setText(promotionInfo.getRedeemInstructions());
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
